﻿import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.0
import QtQuick.Window 2.12

import "../components"

Window {

    id: root

    title: "实验模拟"

    height: 600*dp
    width: 900*dp

    property int frameNum: 0

    property string curExe: "1"

    property bool isRunning: timer.running
    property bool tempIsRunning: false

    property int speed: 1

    onVisibleChanged: {
        if(visible){
            CAnimation.execConfig(1)
            curFrame = 0;
            frameNum = CAnimation.getFrameNum()
            canvas.updateStateFrame(curFrame);
        }
    }


    property int curFrame: 0

    Timer{
        id: timer
        interval: 20
        repeat: true
        onTriggered: {
            curFrame += speed
            canvas.updateStateFrame(curFrame)

            if(curFrame >= frameNum){
                curFrame = frameNum
                timer.stop()
            }

        }
    }


    NuclideCanvas{
        id: canvas
        controller: 1
        anchors.fill: parent
    }

    // 播放进度条
    Rectangle{
        opacity: 0.8
        height: 6*dp;  width: parent.width - 40*dp
        y: parent.height - 30*dp; x: 20*dp
        radius: 3*dp
        Rectangle{      // 进度条2
            width: (parent.width) * (curFrame/(frameNum>0?frameNum:1))
            height: parent.height
            radius: 3*dp
            color: "#1677FF"
        }Rectangle{     // 小圆球
            height: 16*dp
            width: 16*dp
            radius: 8*dp
            border.color: "#CCCCCC"
            x: (parent.width) * (curFrame/(frameNum>0?frameNum:1)) - 8*dp
            y: -5*dp
        }

        MouseArea{
            width: parent.width
            height: 10*dp
            y: -2*dp
            cursorShape: Qt.PointingHandCursor
            onPressed: {
                tempIsRunning = isRunning
                timer.stop()
                var mousePos = mapToItem(this, mouseX, mouseY)
                if(mousePos.x >= 0 && mousePos.x <= width){
                    curFrame = frameNum * (mousePos.x / width)
                }
                canvas.updateStateFrame(curFrame)
            }onPositionChanged: {
                var mousePos = mapToItem(this, mouseX, mouseY)
                if(mousePos.x >= 0 && mousePos.x <= width){
                    curFrame = frameNum * (mousePos.x / width)
                }
                canvas.updateStateFrame(curFrame)
            }onReleased: {
                if(tempIsRunning){
                    timer.start()
                }
            }
        }
    }


    Item{
        y: parent.height - 70*dp
        height: 30*dp; width: parent.width
        Image{      // 暂停/播放
            x: 30*dp
            source: isRunning ? "qrc:/picture/video_pause.png" : "qrc:/picture/video_play.png"
            height: 30*dp; width: 30*dp
            MouseArea{
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onReleased: {
                    if(isRunning){
                        timer.stop()
                    }else{
                        timer.start()
                        if(curFrame >= frameNum){
                            curFrame = 0
                        }
                    }
                }
            }
        }
        Image{      // 停止
            source: "qrc:/picture/video_stop.png"
            x: 70*dp
            height: 30*dp; width: 30*dp
            MouseArea{
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onReleased: {
                    timer.stop()
                    curFrame = 0
                    canvas.updateStateFrame(curFrame)
                }
            }
        }
        Item{
            x: 110*dp
            height: 30*dp; width: 30*dp
            Text{
                text: "x"+speed.toString()
                anchors.centerIn: parent
                font.pixelSize: 24*dp
                color: "white"
                font.bold: true
            }MouseArea{
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onReleased: {
                    speed = speed*2
                    if(speed > 4){
                        speed = 1
                    }
                }
            }

        }

    }



}
